<?php

namespace app\api\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Config;
// use think\facade\Session;
use app\common\model\SystemAdmin;
use app\common\model\User;
use app\common\model\Hotel;
use app\common\model\HotelHome;
use app\common\model\FileLog;
use app\common\controller\Handle;
use app\common\model\Store;
use think\facade\Request;

class ApiHotel extends ApiBase
{
    public function initialize()
    {
        parent::initialize();
        $this->user = new User;
        $this->hotel = new Hotel;
        $this->Store = new Store;
        $this->HotelHome = new HotelHome;
        $this->FileLog = new FileLog;
    }

    //添加酒店
    public function addHotel()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();

        
        if(empty($data['name'])){
            return json(['status' => 0, 'msg' =>"请上传酒店名称"]);
        }


        $add = [
            "name"=>$data['name'],
            "store_id"=>$store_id,
            "created_at"=>date("Y-m-d H:i:s"),
            "update_at"=>date("Y-m-d H:i:s"),
        ];

        if(!empty($data['people'])){
            $add['people'] = $data['people'];
        }

        if(!empty($data['tel'])){
            $add['tel'] = $data['tel'];
        }

        if(!empty($data['others'])){
            $add['others'] = $data['others'];
        }

        if(!empty($data['address'])){
            $add['address'] = $data['address'];
        }

        if(!empty($data['remark'])){
            $add['remark'] = $data['remark'];
        }

        if(!empty($data['img_url'])){
            $add['img_url'] = $data['img_url'];
        }

        $res = $this->hotel->create($add);
        
        return json(['status' => 1, 'msg' => '操作成功']);

    }

    // 酒店列表
    public function listHotel()
    {

        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');


        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        
        $data = request()->post();
        
        $store_id = $is_store['store_id'];

        $where = [];

        $where[] = ["store_id","=",$store_id];
        $where[] = ["deleted_at","=",null];

        if(!empty($data['keyword'])){
            $where[] = ["name","like", "%" . $data['keyword'] . "%"];
        }

        
        if(empty($data['limit'])){
            $data['limit'] = 10;
        }

        if(empty($data['page'])){
            $data['page'] = 1;
        }

        $list = $this->hotel->getPagelist($where,"id,name,people,tel,others,address,img_url,remark",'id desc');

        // foreach($list as $key=>$val){

        //     if(!empty($val['img_url'])){
                
        //         $val->img_url = $domain."/".$val['img_url'];
        //     }
            
        // }

        $count = $this->hotel->countRows($where);

        $pages = ceil($count/$data['limit']);

        return json(['status'=> 1,'data'=>$list,'pages'=>$pages,"count"=>$count]);
    }

      //酒店详情
      public function getHotel()
      {
          // 存储空间对应的域名
          $domain = config('sysconfig.qiniu_domain');
  
          // 判断店铺是否正常
          $is_store = $this->storeIsNormal($this->uid);
  
          if($is_store['code'] == 1){
              return json(['status' => 0, 'msg' =>$is_store['msg']]);
          }
   
          $store_id = $is_store['store_id'];
  
          $data = request()->post();
  
          if(empty($data['id'])){
              return json(['status' => 0, 'msg' =>"请上传酒店ID"]);
          }
  
          $list = $this->hotel->getOne(['id' =>$data['id'],"deleted_at"=>null]);

          
         
  
          if(empty($list)){
              return json(['status' => 0, 'msg' =>"酒店不存在"]);
          }

        //   if(!empty($list['img_url'])){
        //         $list['img_url'] = $domain."/".$list['img_url'];
        //   }
          
          return json(['status'=> 1,'data'=>$list]);
  
  
      }

      //修改酒店
    public function updateHotel()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();
       

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传酒店ID"]);
        }
  
        $list = $this->hotel->getOne(['id' =>$data['id'],"deleted_at"=>null]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"酒店不存在"]);
        }

        if(empty($data['name'])){
            return json(['status' => 0, 'msg' =>"请上传酒店名称"]);
        }


        $save = [
            "name"=>$data['name'],
            "store_id"=>$store_id,
            "update_at"=>date("Y-m-d H:i:s"),
        ];

        if(!empty($data['people'])){
            $save['people'] = $data['people'];
        }else{
            $save['people'] = null;
        }

        if(!empty($data['tel'])){
            $save['tel'] = $data['tel'];
        }else{
            $save['people'] = null;
        }

        if(!empty($data['others'])){
            $save['others'] = $data['others'];
        }else{
            $save['people'] = null;
        }

        if(!empty($data['address'])){
            $save['address'] = $data['address'];
        }else{
            $save['address'] = null;
        }

        if(!empty($data['remark'])){
            $save['remark'] = $data['remark'];
        }else{
            $save['remark'] = null;
        }

        if(!empty($data['img_url'])){
            $save['img_url'] = $data['img_url'];
        }else{
            $save['img_url'] = null;
        }

        $res = $this->hotel->where(['id'=>$data['id']])->update($save);
        
        return json(['status' => 1, 'msg' => '操作成功']);

    }


    // 删除酒店
    public function  delHotel()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();


        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传酒店ID"]);
        }

  
        $list = $this->hotel->getOne(['id' =>$data['id'],"deleted_at"=>null]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"酒店不存在"]);
        }


        $listHome = $this->HotelHome->getOne(['hotel_id' =>$data['id'],"deleted_at"=>null]);

        if(!empty($listHome)){
            return json(['status' => 0, 'msg' =>"请先删除酒店下面的包间"]);
        }

        $res = $this->hotel->where(['id'=>$data['id']])->update(["deleted_at"=>date("Y-m-d H:i:s")]);
        return json(['status' => 1, 'msg' => '操作成功']);
    }

    //添加酒店包间
    public function addHome()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();

        
        if(empty($data['name'])){
            return json(['status' => 0, 'msg' =>"请上传酒店包间名称"]);
        }

        if(empty($data['hotel_id'])){
            return json(['status' => 0, 'msg' =>"请上传酒店ID"]);
        }


        $add = [
            "name"=>$data['name'],
            "hotel_id"=>$data['hotel_id'],
            "created_at"=>date("Y-m-d H:i:s"),
            "update_at"=>date("Y-m-d H:i:s"),
        ];

        if(!empty($data['content'])){
            $add['content'] = $data['content'];
        }

        if(!empty($data['file_url'])){
            $add['file_url'] = json_encode($data['file_url']);
        }else{
            $add['file_url'] = null;
        }

        $res = $this->HotelHome->create($add);
        
        return json(['status' => 1, 'msg' => '操作成功']);

    }

    // 酒店包间列表
    public function listHome()
    {

        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');


        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $data = request()->post();

        if(empty($data['hotel_id'])){
            return json(['status' => 0, 'msg' =>"请上传酒店ID"]);
        }

        $store_id = $is_store['store_id'];

        $where['deleted_at'] = null;
        $where['hotel_id'] = $data['hotel_id'];

        
        if(empty($data['limit'])){
            $data['limit'] = 10;
        }

        if(empty($data['page'])){
            $data['page'] = 1;
        }

        $list = $this->HotelHome->getPagelist($where,"id,name,content",'id desc');

     

        $count = $this->HotelHome->countRows($where);

        $pages = ceil($count/$data['limit']);

        return json(['status'=> 1,'data'=>$list,'pages'=>$pages,"count"=>$count]);
    }

      //酒店包间详情
      public function getHome()
      {
          // 存储空间对应的域名
          $domain = config('sysconfig.qiniu_domain');
  
          // 判断店铺是否正常
          $is_store = $this->storeIsNormal($this->uid);
  
          if($is_store['code'] == 1){
              return json(['status' => 0, 'msg' =>$is_store['msg']]);
          }
   
          $store_id = $is_store['store_id'];
  
          $data = request()->post();
  
          if(empty($data['id'])){
              return json(['status' => 0, 'msg' =>"请上传酒店包间ID"]);
          }
  
          $list = $this->HotelHome->getOne(['id' =>$data['id'],"deleted_at"=>null]);
  
          if(empty($list)){
              return json(['status' => 0, 'msg' =>"酒店包间不存在"]);
          }


          if(!empty($list['file_url'])){
            $imgList = json_decode($list['file_url']);
            // foreach($imgList as $key=>$val){
            //     $imgList[$key] = $domain."/".$val;
                
            // }
            
            $list['img_url'] = $imgList;
          }else{
            $list['img_url'] = [];
          }
          
          return json(['status'=> 1,'data'=>$list]);
  
  
      }

    // 删除酒店包间
    public function  delHome()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();


        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传酒店包间ID"]);
        }

        $list = $this->HotelHome->getOne(['id' =>$data['id'],"deleted_at"=>null]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"酒店包间不存在"]);
        }


        $res = $this->HotelHome->where(['id'=>$data['id']])->update(["deleted_at"=>date("Y-m-d H:i:s")]);
        return json(['status' => 1, 'msg' => '操作成功']);
    }


      //修改酒店包间
    public function updateHome()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();

        
        if(empty($data['name'])){
            return json(['status' => 0, 'msg' =>"请上传酒店包间名称"]);
        }

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传酒店包间ID"]);
        }

        $list = $this->HotelHome->getOne(['id' =>$data['id'],"deleted_at"=>null]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"酒店包间不存在"]);
        }



        $save = [
            "name"=>$data['name'],
            "update_at"=>date("Y-m-d H:i:s"),
        ];

        if(!empty($data['content'])){
            $save['content'] = $data['content'];
        }else{
            $save['content'] = null;
        }
        
        if(!empty($data['file_url'])){
            $save['file_url'] = json_encode($data['file_url']);
        }else{
            $save['file_url'] = null;
        }


        $res = $this->HotelHome->where(['id'=>$data['id']])->update($save);
        
        return json(['status' => 1, 'msg' => '操作成功']);

    }


    // 上传酒店图片或者视频
    public function addFile()
    {
        $file = request()->file('file');

        if (!$file) {
            return ['status' => 0, 'msg' => '上传失败，图片不存在'];
        }

        // 获取文件原始扩展名
        $extension = $file->extension();

        $hand = new Handle();

        $dirname = "api_hotel";

        $savename = $hand->qiniuImg("file", $dirname);

        $add['file_name'] = $savename['fileName'];
        $add['upload_type'] = 2;
        $add['file_path'] = $savename['url'];
        $add['file_type'] = $extension;
        $add['ip'] = Request::ip();
        $add['add_time'] = time();
        $add['user_id'] = $this->uid;
        
        $res = $this->FileLog->create($add);
        $fileLogId = $res->id;
        $savename['file_id'] = $fileLogId;

        return  json($savename);
    }

  

   
    
}
